package com.zzy.demo.config;

import java.nio.charset.Charset;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.ContentVersionStrategy;
import org.springframework.web.servlet.resource.VersionResourceResolver;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**
 * 
 * @author guokaige
 *
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

	@Autowired
	DataInterceptor dataInterceptor;


	/**
	 * 添加json转换
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		// 1.定义一个Converter 转换消息
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		// 2.添加fastjson配置信息, 比如 是否格式化数据
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
		fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
		fastJsonConfig.setCharset(Charset.forName("UTF-8"));
		// 3.在Converter中添加配置信息
		fastConverter.setFastJsonConfig(fastJsonConfig);
		// 4.将Converter 添加到 List converters当中
		converters.add(fastConverter);
	}

	/**
	 * 注册拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		//方便调试, 注销认证
		InterceptorRegistration interceptorRegistration = registry.addInterceptor(dataInterceptor);
		// SpringBoot2.x 静态资源被拦截器拦截
		interceptorRegistration.excludePathPatterns("/static/**");
		interceptorRegistration.excludePathPatterns("/plugin/**");
		interceptorRegistration.excludePathPatterns("/images/**");
		interceptorRegistration.excludePathPatterns("/js/**");
		interceptorRegistration.addPathPatterns("/**");

	}

	/**
	 * 配置静态资源 此处可使用 properties 文件配置
	 * 
	 * @param registry
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		VersionResourceResolver versionResourceResolver = new VersionResourceResolver()
				.addVersionStrategy(new ContentVersionStrategy(), "/**");

		registry.addResourceHandler("/**").addResourceLocations("classpath:/static/","classpath:/upload/")
		.setCachePeriod(2592000)
		.resourceChain(true)
		.addResolver(versionResourceResolver);
		
		registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	}

}
